home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 44 / PC Actual CD 44.iso / Linux / Cygwin / full.exe / Disk1 / data1.cab / Tools / share / tcl8.0 / word.tcl < prev   
Encoding:
Text File  |  1998-12-04  |  4.4 KB  |  137 lines

  1. # word.tcl --
  2. #
  3. # This file defines various procedures for computing word boundaries
  4. # in strings.  This file is primarily needed so Tk text and entry
  5. # widgets behave properly for different platforms.
  6. #
  7. # Copyright (c) 1996 by Sun Microsystems, Inc.
  8. #
  9. # See the file "license.terms" for information on usage and redistribution
  10. # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  11. # SCCS: @(#) word.tcl 1.2 96/11/20 14:07:22
  12. # See the file "license.terms" for information on usage and redistribution
  13. # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  14. #
  15.  
  16. # The following variables are used to determine which characters are
  17. # interpreted as white space.  
  18.  
  19. # CYGNUS local: Always use Motif-style selection
  20. #if {$tcl_platform(platform) == "windows"} {
  21.     # Windows style - any but space, tab, or newline
  22. #    set tcl_wordchars "\[^ \t\n\]"
  23. #    set tcl_nonwordchars "\[ \t\n\]"
  24. #} else {
  25.     # Motif style - any number, letter, or underscore
  26.     set tcl_wordchars {[a-zA-Z0-9_]}
  27.     set tcl_nonwordchars {[^a-zA-Z0-9_]}
  28. #}
  29.  
  30. # tcl_wordBreakAfter --
  31. #
  32. # This procedure returns the index of the first word boundary
  33. # after the starting point in the given string, or -1 if there
  34. # are no more boundaries in the given string.  The index returned refers
  35. # to the first character of the pair that comprises a boundary.
  36. #
  37. # Arguments:
  38. # str -        String to search.
  39. # start -    Index into string specifying starting point.
  40.  
  41. proc tcl_wordBreakAfter {str start} {
  42.     global tcl_nonwordchars tcl_wordchars
  43.     set str [string range $str $start end]
  44.     if [regexp -indices "$tcl_wordchars$tcl_nonwordchars|$tcl_nonwordchars$tcl_wordchars" $str result] {
  45.     return [expr [lindex $result 1] + $start]
  46.     }
  47.     return -1
  48. }
  49.  
  50. # tcl_wordBreakBefore --
  51. #
  52. # This procedure returns the index of the first word boundary
  53. # before the starting point in the given string, or -1 if there
  54. # are no more boundaries in the given string.  The index returned
  55. # refers to the second character of the pair that comprises a boundary.
  56. #
  57. # Arguments:
  58. # str -        String to search.
  59. # start -    Index into string specifying starting point.
  60.  
  61. proc tcl_wordBreakBefore {str start} {
  62.     global tcl_nonwordchars tcl_wordchars
  63.     if {[string compare $start end] == 0} {
  64.     set start [string length $str]
  65.     }
  66.     if [regexp -indices "^.*($tcl_wordchars$tcl_nonwordchars|$tcl_nonwordchars$tcl_wordchars)" [string range $str 0 $start] result] {
  67.     return [lindex $result 1]
  68.     }
  69.     return -1
  70. }
  71.  
  72. # tcl_endOfWord --
  73. #
  74. # This procedure returns the index of the first end-of-word location
  75. # after a starting index in the given string.  An end-of-word location
  76. # is defined to be the first whitespace character following the first
  77. # non-whitespace character after the starting point.  Returns -1 if
  78. # there are no more words after the starting point.
  79. #
  80. # Arguments:
  81. # str -        String to search.
  82. # start -    Index into string specifying starting point.
  83.  
  84. proc tcl_endOfWord {str start} {
  85.     global tcl_nonwordchars tcl_wordchars
  86.     if [regexp -indices "$tcl_nonwordchars*$tcl_wordchars+$tcl_nonwordchars" \
  87.         [string range $str $start end] result] {
  88.     return [expr [lindex $result 1] + $start]
  89.     }
  90.     return -1
  91. }
  92.  
  93. # tcl_startOfNextWord --
  94. #
  95. # This procedure returns the index of the first start-of-word location
  96. # after a starting index in the given string.  A start-of-word
  97. # location is defined to be a non-whitespace character following a
  98. # whitespace character.  Returns -1 if there are no more start-of-word
  99. # locations after the starting point.
  100. #
  101. # Arguments:
  102. # str -        String to search.
  103. # start -    Index into string specifying starting point.
  104.  
  105. proc tcl_startOfNextWord {str start} {
  106.     global tcl_nonwordchars tcl_wordchars
  107.     if [regexp -indices "$tcl_wordchars*$tcl_nonwordchars+$tcl_wordchars" \
  108.         [string range $str $start end] result] {
  109.     return [expr [lindex $result 1] + $start]
  110.     }
  111.     return -1
  112. }
  113.  
  114. # tcl_startOfPreviousWord --
  115. #
  116. # This procedure returns the index of the first start-of-word location
  117. # before a starting index in the given string.
  118. #
  119. # Arguments:
  120. # str -        String to search.
  121. # start -    Index into string specifying starting point.
  122.  
  123. proc tcl_startOfPreviousWord {str start} {
  124.     global tcl_nonwordchars tcl_wordchars
  125.     if {[string compare $start end] == 0} {
  126.     set start [string length $str]
  127.     }
  128.     if [regexp -indices \
  129.         "$tcl_nonwordchars*($tcl_wordchars+)$tcl_nonwordchars*\$" \
  130.         [string range $str 0 [expr $start - 1]] result word] {
  131.     return [lindex $word 0]
  132.     }
  133.     return -1
  134. }
  135.